package com.cb.eduservice.controller;


import com.cb.eduservice.entity.EduChapter;
import com.cb.eduservice.entity.chapter.ChapterVo;
import com.cb.eduservice.service.EduChapterService;
import com.cb.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author Cebb
 * @since 2020-06-22
 */
@Api(description = "章节管理")

@RestController
@RequestMapping("/eduservice/chapter")
public class EduChapterController {

    private final EduChapterService chapterService;

    @Autowired
    public EduChapterController(EduChapterService chapterService) {
        this.chapterService = chapterService;
    }

    @ApiOperation("根据课程id获取章节和小节信息")
    @GetMapping("getChapterAndVideo/{courseId}")
    public R getCharpterAndVideo(@PathVariable String courseId){
        List<ChapterVo> list = chapterService.getChapterAndVideoByCourseId(courseId);

        return R.ok().data("list", list);
    }

    @ApiOperation("添加章节信息")
    @PostMapping
    public R addChapter(@RequestBody EduChapter eduChapter){
        chapterService.save(eduChapter);

        return R.ok();
    }

    @ApiOperation("根据章节id查询章节信息")
    @GetMapping("{chapterId}")
    public R getChapter(@PathVariable String chapterId){
        EduChapter eduChapter = chapterService.getById(chapterId);

        return R.ok().data("info", eduChapter);
    }

    @ApiOperation("修改章节信息")
    @PutMapping("{chapterId}")
    public R updateChapter(@PathVariable String chapterId, @RequestBody EduChapter eduChapter) {
        eduChapter.setId(chapterId);
        chapterService.updateById(eduChapter);

        return R.ok();
    }

    @ApiOperation("根据章节id删除章节信息")
    @DeleteMapping("{chapterId}")
    public R deleteChapter(@PathVariable String chapterId){
        boolean flag = chapterService.deleteChapterById(chapterId);
        if (flag) {
            return R.ok();
        }

        return R.error().message("删除失败, 该章节还存在课程信息 or 章节不存在...");
    }
}

